package gui;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author mrbao
 */
public final class TextBox implements Runnable {

    private String text;
    private int pointer;
    private int width;
    private int x;
    private int y;
    private boolean isSelect = true;
    private int cornerRadius = 6;
    private Font font;
    private boolean isShowPointer = true;
    private int delayTime = 100;
    private Graphics g;

    public TextBox(String text, int width, int x, int y) {
        if (text == null) {
            this.text = "";
        } else {
            this.text = text;
        }
        pointer = this.text.length();
        this.width = width;
        this.x = x;
        this.y = y;
        font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    }

    public void Select(boolean isSelect) {
        this.isSelect = isSelect;
        if (isSelect) {
            new Thread(this).start();
        }
    }

    public void SetGraphics(Graphics g) {
        this.g = g;
    }

    public void run() {
        while (isSelect) {
            try {
//                isShowPointer = !isShowPointer;
                isShowPointer = true;
                paint(g);
                Thread.sleep(delayTime);
            } catch (Exception e) {
            }
        }
    }

    public void paint(Graphics g) {
        g.setColor(60);
        g.drawRoundRect(x, y, width, font.getHeight() + 5, cornerRadius, cornerRadius);
        g.setColor(200, 200, 200);
        g.drawRoundRect(x + 1, y + 1, width - 1, font.getHeight() + 4, cornerRadius, cornerRadius);
        g.setColor(0xffffff);
        g.fillRoundRect(x + 1, y + 1, width - 2, font.getHeight() + 3, cornerRadius, cornerRadius);
        g.setColor(0);
        g.setFont(font);
        if (isSelect && isShowPointer) {
            if (font.substringWidth(text, 0, pointer) > width - 1) {
                g.drawLine(x + width - 2, y + 2, x + width - 2, y + font.getHeight() + 2);
            } else {
                g.drawLine(x + 2 + font.substringWidth(text, 0, pointer), y + 2, x + 2 + font.substringWidth(text, 0, pointer), y + font.getHeight() + 2);
            }
        }
        if (font.stringWidth(text) > width - 1) {
            String str = "";
            int i = 0;
            if (font.substringWidth(text, 0, pointer) > width - 1) {
                while (true) {
                    str = text.substring(i, pointer);
                    if (font.stringWidth(str) <= width - 1) {
                        break;
                    }
                    i++;
                }
                g.drawString(str, x + width - 2, y + 3, Graphics.RIGHT);
            } else {
                i = text.length() - 1;
                while (true) {
                    str = text.substring(0, i);
                    if (font.stringWidth(str) <= width - 1) {
                        break;
                    }
                    i--;
                }
                g.drawString(str, x + 2, y + 3, 0);
            }
        } else {
            g.drawString(text, x + 2, y + 3, 0);
        }
    }

    public void KeyPress(int gameAction, int keyCode) {
        switch (gameAction) {
            case Canvas.LEFT:
                if (pointer > 0) {
                    pointer--;
                }
                break;
            case Canvas.RIGHT:
                if (pointer < text.length()) {
                    pointer++;
                }
                break;
        }
    }
}
